package com.example.springtest.factory;

import com.common.myspring.core.proxy.enums.ProxyEnum;
import com.common.myspring.util.AopUtil;
import com.example.springtest.properties.CarProperty;
import com.example.springtest.service.Car;
import com.example.springtest.service.impl.BWMCar;
import com.example.springtest.service.impl.BenzCar;
import org.springframework.beans.factory.SmartFactoryBean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @title: CarFactoryBean
 * @Author xu-xiang
 * @Date: 2023/8/29 11:41
 * @description:
 */
@Configuration
public class CarFactoryBean implements SmartFactoryBean<Car> {

    private final ThreadLocal<String> brandThreadLocal = new ThreadLocal<>();

    @Resource
    private CarProperty carProperty;

    @Override
    public Car getObject() throws Exception {
        Car car = null;
        if (Objects.equals(carProperty.getType(), "BWM")) {
            car = new BWMCar();
        } else if (Objects.equals(carProperty.getType(), "Benz")) {
            car = new BenzCar();
        } else {
            car = new BWMCar();
        }

        //return car;
        //return getProxyInstance(car);
        //return getCglibInstance(car);
        return AopUtil.getProxyInstance(car, ProxyEnum.JDK);
    }

    @Override
    public Class<?> getObjectType() {
        return Car.class;
    }

    @Override
    public boolean isEagerInit() {
        return true;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}
